﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using System.Data.SqlClient;

namespace LANmine
{
    public partial class SplashForm : Form
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) this.Close();
            return base.ProcessCmdKey(ref msg, keyData);
        }

        public SplashForm()
        {
            InitializeComponent();

            lblLanmine.Text = Application.ProductName;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            /** Disable login button */
            btnLogin.Enabled = false;

            /** Create new User container */
            User lmUser = new User();

            try
            {
                /** Pass on the user container, login ans password to the AuthenticateUser method */
                if (Login.AuthenticateUser(lmUser, this.txtUser.Text, this.txtPass.Text))
                {
                    /**
                     * Login Successful!
                     */
                    
                    /** Empty the login fields */
                    this.txtUser.Clear();
                    this.txtPass.Clear();

                    /** Hide the current form */
                    this.Hide();

                    /** Create an instance of MainForm and pass the User object to it */
                    new MainForm(lmUser).Show();
                }
                else
                {
                    /** Login Failed */
                    lblLogin.Text = "Login failed. Try again.";
                    lblLogin.ForeColor = Color.Red;

                    /** Focus password box */
                    txtPass.Focus();
                    txtPass.SelectAll();
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error connecting to database.\nPlease check the database configuration.\n" + ex.Message);
            }
        }

        private void txtPass_Enter(object sender, EventArgs e)
        {
            /** Select the contents of the TextBox */
            if (txtPass.Text.Length != 0)
                txtPass.SelectAll();

            /** Change the BackColor */
            txtPass.BackColor = Color.Beige;
        }

        private void txtUser_Enter(object sender, EventArgs e)
        {
            /** Select the contents of the TextBox */
            if (txtUser.Text.Length != 0)
                txtUser.SelectAll();

            /** Change the BackColor */
            txtUser.BackColor = Color.Beige;
        }

        private void txtUser_Leave(object sender, EventArgs e)
        {
            /** Reset the back color */
            txtUser.BackColor = Color.White;
        }

        private void txtPass_Leave(object sender, EventArgs e)
        {
            /** Reset the back color */
            txtPass.BackColor = Color.White;
        }

        private void txtPass_TextChanged(object sender, EventArgs e)
        {
            /** Re-enable the login button */
            btnLogin.Enabled = true;
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            /** Re-enable the login button */
            btnLogin.Enabled = true;
        }
    }
}
